package com.yanggeng.demo.beginnerguidedemo.activity

import android.content.Context
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.view.ViewPager
import com.yanggeng.demo.beginnerguidedemo.R
import com.yanggeng.demo.beginnerguidedemo.fragment.TestFragment
import kotlinx.android.synthetic.main.activity_kotlin_test.*

class KotlinTestActivity : BaseActivity() {

    val titles: ArrayList<String> = ArrayList();
    val fragments: ArrayList<Fragment> = ArrayList()
    val imgIds: ArrayList<Int> = ArrayList()
    val context: Context = this

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kotlin_test)
        setTitle(true, "Kotlin Test")
        titles.add("首页")
        titles.add("商城")
        titles.add("发现")
        titles.add("我的")
        fragments.add(TestFragment())
        fragments.add(TestFragment())
        fragments.add(TestFragment())
        fragments.add(TestFragment())
        imgIds.add(R.drawable.selector_tab_home_image)
        imgIds.add(R.drawable.selector_tab_mall_image)
        imgIds.add(R.drawable.selector_tab_home_image)
        imgIds.add(R.drawable.selector_tab_mall_image)
        vp_main.init()
    }

    fun ViewPager.init() {
        val tempAdapter = TempAdapter(supportFragmentManager, fragments, titles, context, imgIds)
        adapter = tempAdapter;
        tab_main.setupWithViewPager(vp_main)
        tab_main.tabMode = TabLayout.MODE_FIXED
        for (i in 0..tab_main.tabCount - 1) {
//            tab_main.getTabAt(i)?.setCustomView(tempAdapter.getTabView(i))
        }
        setCurrentItem(0, true)
    }


    class TempAdapter(fm: FragmentManager?, var list: List<Fragment>, val titles: List<String>, val context: Context, val tabImgs: List<Int>) : FragmentPagerAdapter(fm) {

        override fun getItem(position: Int): Fragment {
            return list.get(position)
        }

        override fun getCount(): Int {
            return list.size;
        }

        override fun getPageTitle(position: Int): CharSequence {
            return titles.get(position)
        }

//        fun getTabView(position: Int): View {
//            val view: View = LayoutInflater.from(context).inflate(R.layout.layout_tab_view, null)
//            val tabIcon = view.findViewById(R.id.tab_icon) as ImageView
//            val tabText = view.findViewById(R.id.tab_text) as TextView
//            tabIcon.setImageResource(tabImgs.get(position))
//            tabText .setText(titles.get(position))
//            if(0 == position) {
//                tabText.setTextColor(Color.BLUE)
//                tabIcon.setImageResource(R.mipmap.ic_launcher)
//            }
//            return view;
//        }

    }


}
